var wrapper = document.querySelector('.wrapper');
var number = 20;
var feiBomb = 0;
var bomb = number * number / 10
var bomb2 = bomb;
bomb = parseInt(bomb);
wrapper.style.width = number * 22 + 'px';
wrapper.style.marginRight = -number * 11 + 'px';
var roundbomb = [];
var q, w;
var again = document.querySelector('.again');
again.addEventListener('click',function(){location.reload(true)})
function expand(l, t) {
    if (roundbomb[number * t + l] == 0) {
        wrapper.children[t].children[l].style.backgroundImage = 'none';
        feiBomb++;
        if(number*number-feiBomb == bomb2){alert('你赢了'); end();}
        if(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))
        {wrapper.children[t].children[l].removeEventListener('touchstart', isBomb, false);}
        else{wrapper.children[t].children[l].removeEventListener('click', isBomb, false);
            wrapper.children[t].children[l].removeEventListener('mouseup', isFlag, false);
        }
        wrapper.children[t].children[l].setAttribute('beidian', 1);
        if (t >= 0 && (l - 1) >= 0) { if (wrapper.children[t].children[l - 1].getAttribute('beidian') == 0) expand(l - 1, t); }
        if (t >= 0 && (l + 1) < number) { if (wrapper.children[t].children[l + 1].getAttribute('beidian') == 0) expand(l + 1, t); }
        if ((t - 1) >= 0 && l >= 0) { if (wrapper.children[t - 1].children[l].getAttribute('beidian') == 0) expand(l, t - 1); }
        if ((t + 1) < number && l >= 0) { if (wrapper.children[t + 1].children[l].getAttribute('beidian') == 0) expand(l, t + 1); }
    }
    else {
        wrapper.children[t].children[l].style.backgroundImage = 'url(./source/number.png)';
        feiBomb++;
        if(number*number-feiBomb == bomb2){alert('你赢了'); end();}
        if(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))
        {wrapper.children[t].children[l].removeEventListener('touchstart', isBomb, false)}
        else{wrapper.children[t].children[l].removeEventListener('click', isBomb, false);
            wrapper.children[t].children[l].removeEventListener('mouseup', isFlag, false);
        }
        wrapper.children[t].children[l].setAttribute('beidian', 1);
        wrapper.children[t].children[l].style.backgroundPosition = -22 * (roundbomb[number * t + l] - 1) + 'px';
    }
}
function end(){
    for (let i = 0; i < bomb2; i++)wrapper.children[bombHang[i]].children[bombLie[i]].style.backgroundImage = 'url(./source/landmine.png)';
    for (let i = 0; i < number; i++) {
        for (let k = 0; k < number; k++) {
            if(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))
                {wrapper.children[i].children[k].removeEventListener('touchstart', isBomb, false);
                }
                else{wrapper.children[i].children[k].removeEventListener('click', isBomb, false);
                wrapper.children[i].children[k].removeEventListener('mouseup', isFlag, false);
                }
        }
    }
}

function isFlag(e) {
    if (e.button == 2) {
        if (this.getAttribute('flag') == 0) {
            this.style.backgroundImage = 'url(./source/flag.png)';
            this.setAttribute('flag',1);
            if(this.className == 'kuai bombbox'){ bomb--;;}
            if(bomb == 0 ){
                end();
                alert('你赢了');
            }
        }
        else {
            this.style.backgroundImage = 'url(./source/kuai.png)';
            this.setAttribute('flag',0);
            if(this.className == 'kuai bombbox')bomb++;
        }
    }
}
function isBomb() {
    if (this.className == 'kuai bombbox') {
        this.style.backgroundImage = 'url(./source/landmine.png)';
        alert('你输了');
        end();
    }
    else {
        q = this.offsetLeft / 22;
        w = this.parentElement.offsetTop / 24;
        expand(Math.ceil(q), Math.ceil(w));
    }
}
document.oncontextmenu = function (event) {
    event.preventDefault();
};
if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
    // console.log('移动端');
    this.isPc=false;
    for (let i = 0; i < number; i++) {
        var div = document.createElement('div');
        wrapper.appendChild(div).className = 'hang';
        var hang = document.querySelectorAll('.hang');
        for (let k = 0; k < number; k++) {
            var div = document.createElement('div');
            div.setAttribute('beidian', 0);
            div.setAttribute('flag',0)
            hang[hang.length - 1].appendChild(div).className = 'kuai';
            div.addEventListener('touchstart', isBomb);

        }
    }
} else {
    this.isPc=true;
    for (let i = 0; i < number; i++) {
        var div = document.createElement('div');
        wrapper.appendChild(div).className = 'hang';
        var hang = document.querySelectorAll('.hang');
        for (let k = 0; k < number; k++) {
            var div = document.createElement('div');
            div.setAttribute('beidian', 0);
            div.setAttribute('flag',0)
            hang[hang.length - 1].appendChild(div).className = 'kuai';
            div.addEventListener('click', isBomb);
            div.addEventListener('mouseup',isFlag);

        }
    }
}
var bombHang = [], bombLie = [];
(function suiji() {
    var getHang;
    var getLie;
    for (let i = 0; i < bomb; i++) {
        setTimeout(function () {

        },2)
        // getHang = Math.floor(Math.random() * 10) * number / 10;
        getHang = Math.floor(Math.random() * number);
        // getLie = Math.floor(Math.random() * 10) * number / 10;
        getLie = Math.floor(Math.random() * number);
        if(wrapper.children[getHang].children[getLie].className == 'kuai bombbox') {i--; }
        else {wrapper.children[getHang].children[getLie].className = 'kuai bombbox';
            bombHang[bombHang.length] = getHang; bombLie[bombLie.length] = getLie;
        }
    }
}());
var roundBombNumber = 0;
(function roundNumber() {
    for (let i = 0; i < number; i++) {
        for (let k = 0; k < number; k++) {
            roundBombNumber = 0;
            if ((i - 1) >= 0) {
                if (wrapper.children[i - 1].children[k].className == 'kuai bombbox') roundBombNumber++;
            }
            if ((k - 1) >= 0) {
                if (wrapper.children[i].children[k - 1].className == 'kuai bombbox') roundBombNumber++;
            }
            if ((i + 1) != number) {
                if (wrapper.children[i + 1].children[k].className == 'kuai bombbox') roundBombNumber++;
            }
            if ((k + 1) != number) {
                if (wrapper.children[i].children[k + 1].className == 'kuai bombbox') roundBombNumber++;
            }
            if ((i - 1) >= 0 && (k - 1) >= 0) {
                if (wrapper.children[i - 1].children[k - 1].className == 'kuai bombbox') roundBombNumber++;
            }
            if (i - 1 >= 0 && k + 1 != number) {
                if (wrapper.children[i - 1].children[k + 1].className == 'kuai bombbox') roundBombNumber++;
            }
            if (i + 1 < number && k - 1 >= 0) {
                if (wrapper.children[i + 1].children[k - 1].className == 'kuai bombbox') roundBombNumber++;
            }
            if ((i + 1) < number && (k + 1) < number) {
                if (wrapper.children[i + 1].children[k + 1].className == 'kuai bombbox') roundBombNumber++;
            }
            roundbomb[roundbomb.length] = roundBombNumber;

        }
    }
}());